package cn.itcast.tant_test;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.LockSupport;

/**
 * @ClassName TestPark
 * @Description TODO
 * @Author tant
 * @Date 2025/5/16 14:54
 */
@Slf4j(topic = "c.TestPark")
public class TestPark {

    public static void main(String[] args) throws InterruptedException {
//        m1();
//        m2();
        m3();

    }


    private static void m1() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            log.debug("开始执行,park住~");
            LockSupport.park();
            log.debug("继续执行");
        }, "t1");
        t1.start();
        Thread.sleep(1000);
        log.debug("unpark-t1");
        LockSupport.unpark(t1);
    }
    //15:05:06.419 c.TestPark [t1] - 开始执行,park住~
    //15:05:07.423 c.TestPark [main] - unpark-t1
    //15:05:07.423 c.TestPark [t1] - 继续执行


    private static void m2() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("开始执行,park住~");
            LockSupport.park();
            log.debug("继续执行");

            log.debug("再park住~");
            LockSupport.park();
            log.debug("再继续执行");
        }, "t1");
        t1.start();
        log.debug("unpark-t1");
        LockSupport.unpark(t1);
    }
//    15:15:36.311 c.TestPark [main] - unpark-t1
//    15:15:37.314 c.TestPark [t1] - 开始执行,park住~
//    15:15:37.314 c.TestPark [t1] - 继续执行
//    15:15:38.029 c.TestPark [t1] - 再park住~


    private static void m3() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            log.debug("开始执行,park住~");
            LockSupport.park();
            log.debug("继续执行...");
        }, "t1");
        t1.start();
        Thread.sleep(1000);
        log.debug("打断t1~");
        t1.interrupt();
        log.debug("t1打断状态：{}", t1.isInterrupted());
    }
//            15:18:26.222 c.TestPark [t1] - 开始执行,park住~
//            15:18:27.219 c.TestPark [main] - 打断t1~
//            15:18:27.219 c.TestPark [t1] - 继续执行...
//            15:18:27.219 c.TestPark [main] - t1打断状态：true

}

    